package com.sctvcloud.renhe.ui.utils;

import android.content.Context;
import android.support.annotation.NonNull;

import com.jiongbull.jlog.JLog;
import com.sctvcloud.renhe.base.BaseFragment;
import com.sctvcloud.renhe.ui.adapter.GreatestAdapter;
import com.sctvcloud.renhe.ui.adapter.holder.HomeLiveHolder;
import com.sctvcloud.renhe.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/7.
 */

public class GreatestDataAddHelper {
    private HomeLiveHolder.OnLivePlayingListener playingListener;
    private GreatestAdapter adapter;
    private BaseFragment fragment;
    private Object syncObj;

    private int bannerPos = -1, expressPos = -1, livePos = -1, posularPos = -1, spcialPos = -1, news_1 = -1, news_2 = -1, news_3 = -1, anchorPos = -1, upPos;

    public GreatestDataAddHelper() {
        syncObj = new Object();
    }

    private void initAdapter(Context context, List<IListShowData> data, BaseFragment fragment, HomeLiveHolder.OnLivePlayingListener listener) {
        adapter = new GreatestAdapter(context, data, fragment,listener);
        bannerPos = -1;
        expressPos = -1;
        livePos = -1;
        posularPos = -1;
        spcialPos = -1;
        news_1 = -1;
        news_2 = -1;
        news_3 = -1;
        anchorPos = -1;
        upPos = -1;
    }

    private void initsAdapter(Context context, List<? extends IListShowData> data, BaseFragment fragment, HomeLiveHolder.OnLivePlayingListener listener) {
        adapter = new GreatestAdapter(context, data, fragment,listener);
        bannerPos = -1;
        expressPos = -1;
        livePos = -1;
        posularPos = -1;
        spcialPos = -1;
        news_1 = -1;
        news_2 = -1;
        news_3 = -1;
        anchorPos = -1;
        upPos = -1;
    }

    private void initAdapter(@NonNull IListShowData data, Context context, BaseFragment fragment, HomeLiveHolder.OnLivePlayingListener listener) {
        ArrayList<IListShowData> list = new ArrayList<>();
        list.add(data);
        adapter = new GreatestAdapter(context, list, fragment,listener);
        bannerPos = -1;
        expressPos = -1;
        livePos = -1;
        posularPos = -1;
        spcialPos = -1;
        news_1 = -1;
        news_2 = -1;
        news_3 = -1;
        anchorPos = -1;
        upPos = -1;
    }

    public GreatestAdapter getAdapter(Context context, BaseFragment fragment, HomeLiveHolder.OnLivePlayingListener listener) {
        this.fragment = fragment;
        playingListener = listener;
        synchronized (syncObj) {
            if (adapter == null) {
                initAdapter(context, null, fragment,playingListener);
            }
        }
        return adapter;
    }

    public void setBannerData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            bannerPos = addOrRefresh(context, GreatestAdapter.VIEWTYPE_BANNER - 1, bannerPos, data);
            freshOtherPos(GreatestAdapter.VIEWTYPE_BANNER, bannerPos, 1);
        }
//        if (adapter == null) {
//            initAdapter(data, context);
//            bannerPos = 0;
//        } else if (bannerPos == -1) {
//            bannerPos = adapter.addData(data, 0);
//        } else {
//            int pos = adapter.setDataInPos(data, bannerPos, false);
//            if (pos >= 0) {
//                bannerPos = pos;
//            }
//        }
    }

    public void setExpressData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            expressPos = addOrRefresh(context, GreatestAdapter.VIEWTYPE_EXPRESS - 1, expressPos, data);
            freshOtherPos(GreatestAdapter.VIEWTYPE_EXPRESS, expressPos, 1);
        }
    }

    public void setLiveData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            livePos = addOrRefresh(context, GreatestAdapter.VIEWTYPE_LIVE - 1, livePos, data);
            if (livePos >= 0) {
                upPos = livePos;
                freshOtherPos(GreatestAdapter.VIEWTYPE_LIVE, livePos, 1);
            }
        }
    }

    public void setPopularData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            posularPos = addOrRefresh(context, GreatestAdapter.VIEWTYPE_POPULAR - 1, posularPos, data);
            if (posularPos >= 0) {
                upPos = posularPos;
                freshOtherPos(GreatestAdapter.VIEWTYPE_POPULAR, posularPos, 1);
            }
        }
    }

    public void setSpecialData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            spcialPos = addOrRefresh(context, GreatestAdapter.VIEWTYPE_SPECIAL - 1, spcialPos, data);
            if (spcialPos >= 0) {
                upPos = spcialPos;
                freshOtherPos(GreatestAdapter.VIEWTYPE_SPECIAL, spcialPos, 1);
            }
        }
    }

    public void addTop24DataTopOne(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            news_1 = addOrRefresh(context, GreatestAdapter.VIEWTYPE_TOP1_NEWS - 1, news_1, data);
            if (news_1 >= 0) {
                upPos = news_1;
                freshOtherPos(GreatestAdapter.VIEWTYPE_TOP1_NEWS, news_1, 1);
            }
        }
    }

    public void addTop24DataTopTwo(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            news_2 = addOrRefresh(context, GreatestAdapter.VIEWTYPE_TOP2_NEWS - 1, news_2, data);
            if (news_2 >= 0) {
                upPos = news_2;
                freshOtherPos(GreatestAdapter.VIEWTYPE_TOP2_NEWS, news_2, 1);
            }
        }
    }

    public void addTop24DataTopThree(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            news_3 = addOrRefresh(context, GreatestAdapter.VIEWTYPE_TOP3_NEWS - 1, news_3, data);
            if (news_3 >= 0) {
                upPos = news_3;
                freshOtherPos(GreatestAdapter.VIEWTYPE_TOP3_NEWS, news_3, 1);
            }
        }
    }

    public void setAnchorData(Context context, IListShowData data) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            anchorPos = addOrRefresh(context, GreatestAdapter.VIEWTYPE_ANCHORS - 1, anchorPos, data);
            if (anchorPos >= 0) {
                upPos = anchorPos;
                freshOtherPos(GreatestAdapter.VIEWTYPE_ANCHORS, anchorPos, 1);
            }
        }
    }

    /**
     * 刷新页面数据
     *
     * @param context
     * @param data
     * @param mode
     */
    public void setUpDatas(Context context, IListShowData data, int mode) {
        JLog.e("setUpDatas---mode-", "--mode=" + mode);
        switch (mode) {
            case GreatestAdapter.VIEWTYPE_BANNER:
                setBannerData(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_EXPRESS:
                setExpressData(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_LIVE:
                setLiveData(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_POPULAR:
                setPopularData(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_TOP1_NEWS:
                addTop24DataTopOne(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_TOP2_NEWS:
                addTop24DataTopTwo(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_TOP3_NEWS:
                addTop24DataTopThree(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_SPECIAL:
                setSpecialData(context, data);
                break;
            case GreatestAdapter.VIEWTYPE_ANCHORS:
                setAnchorData(context, data);
                break;

        }
    }

    //    public void addOrSet24Datas(Context context, List<IListShowData> datas, boolean isReset) {
//        if (datas == null || datas.size() == 0) {
//            return;
//        }
//        synchronized (syncObj) {
//            if (adapter == null) {
//                initAdapter(context, datas);
//            } else if (isReset) {
//                if (upPos < 0) {
//                    adapter.setData(datas);
//                } else {
//                    adapter.resetDatasFrom(upPos, datas, true);
////                    if (upPos>=adapter.getItemCount()) {
////                        adapter.addDatas(datas);
////                    }else {
////                    }
//                }
//            } else {
//                adapter.addDatas(datas);
//            }
//        }
//    }

    public void addOrSet24Datass(Context context, List<? extends IListShowData> datas, boolean isReset) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        JLog.e("poss banner= " + bannerPos + "  express=" + expressPos + "  live=" + livePos + "  popular=" + posularPos + "\n up=" + upPos);
        synchronized (syncObj) {
            if (adapter == null) {
                initsAdapter(context, datas, fragment,playingListener);
            } else if (isReset) {
                if (upPos < 0) {
                    adapter.setDatass(datas);
                } else {
                    adapter.resetDatasFroms(upPos, datas, true);
//                    if (upPos>=adapter.getItemCount()) {
//                        adapter.addDatas(datas);
//                    }else {
//                    }
                }
            } else {
                int pos = adapter.getItemCount();
                adapter.addDatass(datas);
                if (pos > 0) {
                    adapter.notifyItemChanged(pos - 1);
                }
            }
        }
    }

    public void remove24data() {
        synchronized (syncObj) {
            if (adapter == null || adapter.getItemCount() == 0 || (upPos >= 0 && upPos == adapter.getItemCount() - 1)) {
                return;
            }
            if (upPos < 0) {
                adapter.clean();
            } else {
                adapter.removeFrom(upPos + 1);
            }
        }
    }

    /**
     * 清理精选栏目数据，只保留banner
     */
    public void cleanGreatestdata() {
        synchronized (syncObj) {
            if (adapter == null || adapter.getItemCount() == 0 || (upPos >= 0 && upPos == adapter.getItemCount() - 1)) {
                return;
            }
            adapter.removeFrom(0);
        }
    }

    private int getUpInsertPos(int targetPos) {
        if (targetPos == 0) {
            return targetPos;
        }
        if (upPos == -1) {
            return 0;
        }
        switch (targetPos + 1) {
            case GreatestAdapter.VIEWTYPE_BANNER:
                if (livePos >= 0) {
                    return livePos;
                } else {
                    targetPos = 0;
                }
                break;
            case GreatestAdapter.VIEWTYPE_LIVE:
                if (news_2 >= 0) {
                    return news_2;
                } else {
                    targetPos = 1;
                }
                break;
//            case GreatestAdapter.VIEWTYPE_SPECIAL:
//                if (news_2 >= 0) {
//                    return news_2;
//                }else{
//                    targetPos = 1;
//                }
//                break;
            case GreatestAdapter.VIEWTYPE_TOP2_NEWS:
                if (posularPos >= 0) {
                    return posularPos;
                } else {
                    targetPos = 2;
                }
                break;
            case GreatestAdapter.VIEWTYPE_POPULAR:
                if (news_3 >= 0) {
                    return news_3;
                } else {
                    targetPos = 4;
                }
                break;
            case GreatestAdapter.VIEWTYPE_TOP3_NEWS:
                if (anchorPos >= 0) {
                    return anchorPos;
                } else {
                    targetPos = 5;
                }
                break;
//            case GreatestAdapter.VIEWTYPE_ANCHORS:
//                if (posularPos >= 0) {
//                    return posularPos;
//                }
//                break;
            default:
                if (targetPos >= upPos) {
                    return upPos;
                }

        }
        return targetPos;
    }

    private synchronized int addOrRefresh(Context context, int targetPos, int temPos, @NonNull IListShowData data) {
//        JLog.e("poss befor banner= " + bannerPos + "  express=" + expressPos + "  live=" + livePos + "  popular="
//                + posularPos + "\n up=" + upPos + "  target=" + targetPos + "  temp=" + temPos);
        if (targetPos == -1) {
            return temPos;
        }
        if (adapter == null) {
            initAdapter(data, context, fragment,playingListener);
            return 0;
        } else {
            if (temPos == -1) {
                temPos = adapter.addData(data, getUpInsertPos(targetPos));
            } else {
                int pos = adapter.setDataInPos(data, temPos, false);
                if (pos >= 0) {
                    temPos = pos;
                }
            }
        }
        return temPos;
    }

    public void removeUpItem(int mode) {
        synchronized (syncObj) {
            if (adapter == null || mode < 0 || mode > GreatestAdapter.VIEWTYPE_ANCHORS) {
                return;
            } else {
                int pos = -1;
                switch (mode) {
                    case GreatestAdapter.VIEWTYPE_BANNER:
                        pos = bannerPos;
                        break;
                    case GreatestAdapter.VIEWTYPE_EXPRESS:
                        pos = expressPos;
                        break;
                    case GreatestAdapter.VIEWTYPE_LIVE:
                        pos = livePos;
                        break;
                    case GreatestAdapter.VIEWTYPE_POPULAR:
                        pos = posularPos;
                        break;
                    case GreatestAdapter.VIEWTYPE_TOP1_NEWS:
                        pos = news_1;
                        break;
                    case GreatestAdapter.VIEWTYPE_TOP2_NEWS:
                        pos = news_2;
                        break;
                    case GreatestAdapter.VIEWTYPE_TOP3_NEWS:
                        pos = news_3;
                        break;
                    case GreatestAdapter.VIEWTYPE_SPECIAL:
                        pos = spcialPos;
                        break;
                    case GreatestAdapter.VIEWTYPE_ANCHORS:
                        pos = anchorPos;
                        break;
                }
                if (adapter.removeByPostion(pos)) {
                    freshOtherPos(mode, pos, -1);
                }
            }
        }
    }

    private synchronized void freshOtherPos(int mode, int pos, int diff) {
        boolean isAdd = diff > 0;
        switch (mode) {
            case GreatestAdapter.VIEWTYPE_BANNER:
                if (livePos >= 0) {
                    if (isAdd) {
                        if (livePos <= pos) {
                            livePos++;
                        }
                    } else {
                        livePos--;
                        bannerPos = diff;
                        diff = livePos;
                    }
                    pos = livePos;
                }
//            case GreatestAdapter.VIEWTYPE_TOP1_NEWS:
//                if (spcialPos >= 0) {
//                    if (isAdd) {
//                        if (spcialPos <= pos) {
//                            spcialPos++;
//                        }
//                    } else {
//                        spcialPos--;
//                        news_1 = diff;
//                        diff = spcialPos;
//                    }
//                    pos = spcialPos;
//                }
//            case GreatestAdapter.VIEWTYPE_SPECIAL:
//                if (news_2 >= 0) {
//                    if (isAdd) {
//                        if (news_2 <= pos) {
//                            news_2++;
//                        }
//                    } else {
//                        news_2--;
//                        spcialPos = diff;
//                        diff = news_2;
//                    }
//                    pos = news_2;
//                }
            case GreatestAdapter.VIEWTYPE_LIVE:
                if (news_2 >= 0) {
                    if (isAdd) {
                        if (news_2 <= pos) {
                            news_2++;
                        }
                    } else {
                        news_2--;
                        livePos = diff;
                        diff = news_2;
                    }
                    pos = news_2;
                }
            case GreatestAdapter.VIEWTYPE_TOP2_NEWS:
                if (posularPos >= 0) {
                    if (isAdd) {
                        if (posularPos <= pos) {
                            posularPos++;
                        }
                    } else {
                        posularPos--;
                        news_2 = diff;
                        diff = posularPos;
                    }
                    pos = posularPos;
                }

//            case GreatestAdapter.VIEWTYPE_EXPRESS:
//                if (livePos >= 0) {
//                    if (isAdd) {
//                        if (livePos <= pos) {
//                            livePos++;
//                        }
//                    } else {
//                        livePos--;
//                        expressPos = diff;
//                        diff = livePos;
//                    }
//                    pos = livePos;
//                }

            case GreatestAdapter.VIEWTYPE_POPULAR:
                if (news_3 >= 0) {
                    if (isAdd) {
                        if (news_3 <= pos) {
                            news_3++;
                        }
                    } else {
                        news_3--;
                        posularPos = diff;
                        diff = news_3;
                    }
                    pos = news_3;
                }
            case GreatestAdapter.VIEWTYPE_TOP3_NEWS:
                if (anchorPos >= 0) {
                    if (isAdd) {
                        if (anchorPos <= pos) {
                            anchorPos++;
                        }
                    } else {
                        anchorPos--;
                        news_3 = diff;
                        diff = anchorPos;
                    }
                    pos = anchorPos;
                }
                upPos = pos;
            case GreatestAdapter.VIEWTYPE_ANCHORS:
                if (!isAdd) {
                    anchorPos = -1;
                    upPos--;
                } else {
                    upPos++;
                }
                break;

        }
        JLog.e("poss af banner= " + bannerPos + "  express=" + expressPos + "  live=" + livePos + "  popular="
                + posularPos + "\n up=" + upPos + "  mode=" + mode);
    }

    public void onDestory() {
    }

    public boolean hasDatas() {
        return adapter != null && adapter.getItemCount() > 0;
    }
}
